/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.mailet;

import static cc.aileron.commons.global.GlobalSetting.*;
import java.io.IOException;

import cc.aileron.commons.exception.SettingException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.commons.utility.ResourceUtils;
import cc.aileron.mailet.daemon.MailetDaemonLogging;
import cc.aileron.mailet.daemon.MailetDaemonLoggingImpl;
import cc.aileron.mailet.daemon.MailetDaemonSetting;

import com.google.inject.Binder;
import com.google.inject.Module;

/**
 * アプリケーションモジュール
 * @author Aileron
 *
 */
public class ApplicationModule implements Module
{
    private static final String GLOBAL_SETTING_FILE_PATH = "/opt/setting/aileron-framework-mailet.properties";
    
    /**
     * @throws ResourceNotFoundException 
     * @throws IOException 
     * @throws SettingException 
     * 
     */
    public ApplicationModule(
            final MailetSetting setting,
            final Class<? extends Mailet> mailetClass) 
    throws IOException, ResourceNotFoundException, SettingException
    {
        globalSettingProperties(ResourceUtils.resource(GLOBAL_SETTING_FILE_PATH).toProperties());
        if(setting==null)
        {
            throw new SettingException("setting instance is null");
        }

        final String host = setting.host;
        final String user = setting.user;
        final String pass = setting.pass;
        
        if(host==null || user==null || pass==null)
        {
            final StringBuilder sb = new StringBuilder();
            sb.append("setting key is not found [");
            sb.append(host==null ? " mail.host" : null);
            sb.append(user==null ? " mail.user" : null);
            sb.append(pass==null ? " mail.pass" : null);
            sb.append("]");
            throw new SettingException(sb.toString());
        }
        this.setting = new MailetDaemonSetting(host,user,pass);
        this.mailetClass = mailetClass;
    }
    
    /**
     * mailet
     */
    private final Class<? extends Mailet> mailetClass;
    
    /**
     * setting
     */
    private final MailetDaemonSetting setting;
    
    /*
     * (非 Javadoc)
     * @see com.google.inject.Module#configure(com.google.inject.Binder)
     */
    public void configure(final Binder binder)
    {
        binder.bind(MailetDaemonSetting.class).toInstance(setting);
        binder.bind(MailetDaemonLogging.class).to(MailetDaemonLoggingImpl.class);
        binder.bind(Mailet.class).to(mailetClass);
    }
}